//
//  ZHWelcomeController.swift
//  ZHWeibo
//
//  Created by user on 15/12/9.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let ZHSwitchRootVCNotifation = "ZHSwitchRootVCNotifation"
let FromWelcomeToHome = "FromWelcomeToHome"

class ZHWelcomeController: UIViewController {
    
    
    override func loadView() {
        //super.loadView()
        printLog("欢迎页面");
        //初始化大View
        view = backImageView
        /**
          * 建议加一个背景色
        */
        view.backgroundColor = UIColor.whiteColor()
        setUpUI()
        //初始化其他控件
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    private func setUpUI() {
        view.addSubview(headImageView)
        view.addSubview(noticeLabel)
        
        //添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            //添加约束的时候,一定要将约束添加完整
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(20)
            make.centerX.equalTo(headImageView)
        }
        
    }
    //在ViewdidAppear中修改约束,并为约束设置动画
    //因为noticeLabel的约束完全取决于headImageView,所以只需要修改headImageView的约束即可
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        /**
        *  这里应该使用snap的update方法
        */
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(150)
        }
        noticeLabel.alpha = 0
        //动画中去刷新
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //注意刷新的view,block中必须使用self
            self.view.layoutIfNeeded()
            /**
            *  这里为什么可以使用下划线
            */
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    //这里不许要刷新是因为不是取刷新约束的更新
                    self.noticeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //切换控制器
                        dispatch_after(UInt64(10), dispatch_get_main_queue()) { () -> Void in
                            //跳转控制器----发送通知
                            NSNotificationCenter.defaultCenter().postNotificationName(FromWelcomeToHome, object: nil)
                        }
                })
        }
    }

    //懒加载控件
    lazy private var headImageView:UIImageView = {
        /// 这种直接初始化,不需要设置imageView的frame
        let headImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //这里不需要判断路径是否存在吗?
        headImageView.sd_setImageWithURL(NSURL(string:ZHUserAccountViewModel.shareAccountInstance.userAccount!.avatar_large!)!, placeholderImage: UIImage(named: "avatar_default_big"))
        headImageView.cornerRadius = 45;
        return headImageView
    }()
    lazy private var noticeLabel:UILabel = {
        let label:UILabel = UILabel()
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.darkGrayColor()
        label.text = "欢迎回来"
        return label
    }()
    lazy private var backImageView:UIImageView = {
        //根据屏幕大小取获取图片
        return UIImageView(image: UIImage(named: "ad_background"))
    }()
}
